<template>
  <div class="auth-test">
    <el-card>
      <template #header>
        <h3>认证状态测试</h3>
      </template>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-button @click="testAuth" type="primary" style="width: 100%">检查认证状态</el-button>
        </el-col>
        <el-col :span="12">
          <el-button @click="testProfile" type="success" style="width: 100%">测试个人信息接口</el-button>
        </el-col>
      </el-row>
      
      <el-row :gutter="20" style="margin-top: 10px;">
        <el-col :span="12">
          <el-button @click="testTeacherAPI" type="info" style="width: 100%">测试教师API</el-button>
        </el-col>
        <el-col :span="12">
          <el-button @click="showLocalStorage" type="warning" style="width: 100%">显示本地存储</el-button>
        </el-col>
      </el-row>
      
      <el-row style="margin-top: 10px;">
        <el-col :span="24">
          <el-input v-model="loginUsername" placeholder="用户名" style="margin-bottom: 10px;" />
          <el-input v-model="loginPassword" type="password" placeholder="密码" style="margin-bottom: 10px;" />
          <el-button @click="doLogin" type="danger" style="width: 100%">登录测试</el-button>
        </el-col>
      </el-row>
      
      <div class="result" v-if="result">
        <h4>测试结果：</h4>
        <pre>{{ JSON.stringify(result, null, 2) }}</pre>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { ElMessage } from 'element-plus'
import userService from '@/api/user'
import { login } from '@/api/auth'
import axios from 'axios'

export default defineComponent({
  name: 'AuthTest',
  setup() {
    const result = ref<any>(null)
    const loginUsername = ref('马德明')
    const loginPassword = ref('123456')
    
    const testAuth = async () => {
      try {
        const response = await userService.testAuthStatus()
        result.value = response.data
        ElMessage.success('认证状态检查完成')
      } catch (error: any) {
        result.value = { error: error.message, response: error.response?.data }
        ElMessage.error('认证状态检查失败')
      }
    }
    
    const testProfile = async () => {
      try {
        const response = await userService.getCurrentUserProfile()
        result.value = { profile: response.data }
        ElMessage.success('个人信息获取成功')
      } catch (error: any) {
        result.value = { error: error.message, response: error.response?.data }
        ElMessage.error('个人信息获取失败')
      }
    }
    
    const testTeacherAPI = async () => {
      try {
        // 测试教师API
        const [studentsRes, pendingRes, reviewedRes] = await Promise.allSettled([
          axios.get('/api/teacher/students'),
          axios.get('/api/teacher/achievements/pending'),
          axios.get('/api/teacher/achievements/reviewed')
        ])
        
        result.value = {
          studentsAPI: studentsRes.status === 'fulfilled' ? studentsRes.value.data : studentsRes.reason?.response?.data,
          pendingAPI: pendingRes.status === 'fulfilled' ? pendingRes.value.data : pendingRes.reason?.response?.data,
          reviewedAPI: reviewedRes.status === 'fulfilled' ? reviewedRes.value.data : reviewedRes.reason?.response?.data
        }
        ElMessage.success('教师API测试完成')
      } catch (error: any) {
        result.value = { error: error.message, response: error.response?.data }
        ElMessage.error('教师API测试失败')
      }
    }
    
    const showLocalStorage = () => {
      const user = localStorage.getItem('user')
      const userRole = localStorage.getItem('userRole')
      const parsedUser = user ? JSON.parse(user) : null
      
      result.value = {
        localStorage: {
          user: parsedUser,
          userRole: userRole,
          rawUser: user
        },
        sessionStorage: {
          // 显示所有sessionStorage内容
          ...Object.fromEntries(Object.entries(sessionStorage))
        },
        cookies: document.cookie
      }
      ElMessage.info('本地存储信息已显示')
    }
    
    const doLogin = async () => {
      try {
        const response = await login(loginUsername.value, loginPassword.value)
        result.value = { loginSuccess: true, userData: response.data }
        ElMessage.success('登录成功')
      } catch (error: any) {
        result.value = { loginError: error.message, response: error.response?.data }
        ElMessage.error('登录失败')
      }
    }
    
    return {
      result,
      loginUsername,
      loginPassword,
      testAuth,
      testProfile,
      testTeacherAPI,
      showLocalStorage,
      doLogin
    }
  }
})
</script>

<style scoped>
.auth-test {
  max-width: 800px;
  margin: 20px auto;
}

.result {
  margin-top: 20px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

pre {
  white-space: pre-wrap;
  word-break: break-all;
}
</style> 